﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Albon.Model;
namespace AlbonWeb.Manager.MessageManager
{
    public partial class MessageNew : System.Web.UI.Page
    {
        private readonly BLL.MessageBLL messagebll = new BLL.MessageBLL();
        private readonly BLL.UserBLL userbll = new BLL.UserBLL(); 
        private readonly BLL.OrderBLL orderbll = new BLL.OrderBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ViewState["Back"] = Request.UrlReferrer.ToString();
                if (Request["orderID"] != null)
                {
                    DataTable dt = orderbll.getOrderByID(Request["orderid"].ToString());
                    if (User.Identity.Name != dt.Rows[0]["userName"].ToString())
                    {
                        tbReceiver.Text = dt.Rows[0]["userName"].ToString();
                        tbReceiver.Enabled = false;
                        ddlReceiver.Visible = false;
                        arrow.Visible = false;
                    }
                    tbRef.Text = dt.Rows[0]["refNo"].ToString();
                    tbRef.Enabled = false;
                }
                else
                {
                    refNo.Visible = false;
                }
                bindReceiver(userbll.getRoleID(User.Identity.Name).ToString());  
            }
        }
        //绑定收件人
        protected void bindReceiver(string roleid)
        {
            string cmdstr = "select * from UserAccount where active=1 and roleID!=" + roleid+" and userName!='admin' order by roleID asc";
            Commonlib.SqlHelper.BindDropDownList(ddlReceiver, "userName", "ID", Commonlib.SqlHelper.SqlConnectionString, CommandType.Text, cmdstr, null);
            ddlReceiver.Items.Insert(0, new ListItem("--receiver--", "0"));
        }
        
        //发送信息
        protected void addMessage_Click(object sender, EventArgs e)
        {
            Message message = new Message();
            message.ID = 0;
            if (Request["orderid"] != null)
            {
                message.orderID = int.Parse(Request["orderid"].ToString());
            }
            else {
                message.orderID = 0;
            }
            message.refID = 0;
            message.sender = User.Identity.Name;
            message.receiver = tbReceiver.Text;
            message.title = tbTitle.Text.Trim();
            message.content = tbContent.Text;
            message.isMarked = isMarked.Checked;
            message.EmailAddress = "";

            messagebll.addMessage(message);
            Response.Redirect(ViewState["Back"].ToString());
        }

        //选择收件人
        protected void ddlReceiver_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlReceiver.SelectedIndex != 0)
            {
                tbReceiver.Text = ddlReceiver.SelectedItem.Text;
            }
        }
    }
}